home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / IBROW.ICN < prev    next >
Text File  |  1992-11-26  |  5KB  |  183 lines

  1. ############################################################################
  2. #
  3. #    File:     ibrow.icn
  4. #
  5. #    Subject:  Program to browse Icon files for declarations
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     September 7, 1990
  10. #
  11. ###########################################################################
  12. #
  13. #  Usage: ibrow [<Icon source file name>...]
  14. #
  15. #  If no source file names are provided on the command line, all *.icn
  16. #  files in the current directory are browsed.
  17. #
  18. #  The program facilitates browsing of Icon programs.  It was originally
  19. #  written to browse the Icon Program Library, for which purpose it
  20. #  serves quite well.  The user interface is self-explanatory -- just
  21. #  remember to use "?" for help if you're confused.
  22. #
  23. ############################################################################
  24. #
  25. #  Links:  colmize
  26. #
  27. ############################################################################
  28. #
  29. #  Requires:  UNIX
  30. #
  31. ############################################################################
  32.  
  33. link colmize
  34.  
  35. procedure main(arg)
  36.    local p, proctab, doneNames, fn, f, foundNonEmptyLine, block, lineNbr
  37.    local line, keywd, startLine, proclist, w, i, x, proclines, cmd, b
  38.  
  39.    if not (&features == "UNIX") then stop("Runs only under UNIX")
  40.    if *arg = 0 then {
  41.       p := open("ls *.icn","rp")
  42.       while put(arg,read(p))
  43.       close(p)
  44.       }
  45.    proctab := table()
  46.    #
  47.    #  Loop to scan all of the specified source files and save their
  48.    #  procedures and records.
  49.    #
  50.    doneNames := set()    # This set is used to prevent scanning twice if
  51.             # both a source and a suffixless icode file are
  52.             # passed as arguments (e.g. mydir/*).
  53.    write("Icon Browser -- scanning files:")
  54.    every fn := !arg do {
  55.       if not (fn[-4:0] == ".icn") then fn ||:= ".icn"
  56.       if member(doneNames,fn) then next
  57.       insert(doneNames,fn)
  58.       f := if fn == "-" then &input else open(fn) | next
  59.       write("  ",fn)
  60.       #
  61.       #  Loop to process lines of file (in string scanning mode).
  62.       #
  63.       foundNonEmptyLine := &null
  64.       block := []
  65.       lineNbr := 0
  66.       while line := read(f) do line ? {
  67.      lineNbr +:= 1
  68.      if not pos(0) then {
  69.         foundNonEmptyLine := 1
  70.         if (tab(many(' \t')) | "")\1 &
  71.           (keywd := =("end" | "global" | "link")\1) |
  72.           (keywd := =("procedure" | "record")\1 &
  73.           tab(many(' \t')) & name := tab(upto(' \t('))\1) then {
  74.            if keywd == ("procedure" | "record") then startLine := lineNbr
  75.            if keywd == "record" then {
  76.           until find(")",line) do {
  77.              put(block,line)
  78.              line := read(f) | break
  79.              lineNbr +:= 1
  80.              }
  81.           }
  82.            if proctab[name || case keywd of {"end": "()"; "record": "."}] :=
  83.              [block,fn,startLine] then put(block,line)
  84.            if keywd ~== "procedure" then {
  85.           foundNonEmptyLine := &null
  86.           block := []
  87.           }
  88.            }
  89.         }
  90.      if \foundNonEmptyLine then put(block,line)
  91.      }
  92.       #
  93.       #  Close this file.
  94.       #
  95.       close(f)
  96.       }
  97.    doneNames := &null
  98.    #
  99.    #  Reorganize the data.
  100.    #
  101.    proctab := sort(proctab)
  102.    proclist := []
  103.    w := **proctab
  104.    i := 0
  105.    every x := !proctab do
  106.      put(proclist,right(i +:= 1,w) || ". " || x[1])
  107.    proclines := []
  108.    every put(proclines,colmize(proclist))
  109.    proclist := []
  110.    every put(proclist,(!proctab)[2])
  111.    proctab := &null
  112.    #
  113.    #  Interact with the user to browse.
  114.    #
  115.    repeat {
  116.       write()
  117.       every write(!proclines)
  118.       write()
  119.       repeat {
  120.      #
  121.      #  Prompt for, read, and analyze the user's command.
  122.      #
  123.      writes("\nq,nn,nn[fmev],<return> (? for help): ")
  124.      line := read() | exit()
  125.      case line of {
  126.        "q": exit()
  127.        "?": help() & next
  128.        "":  break
  129.      }
  130.      if integer(line) then line ||:= "f"
  131.      if cmd := line[-1] & any('fmev',cmd) &
  132.            block := proclist[0 < integer(line[1:-1])] then {
  133.         case cmd of {
  134.            "f": {
  135.           #
  136.           #  Write the file name containing the procedure and the
  137.           #  first line of the procedure.
  138.           #
  139.           b := block[1]
  140.           every line := b[1 to *b] do {
  141.              line ? (if (tab(many(' \t')) | "")\1 &
  142.                =("procedure" | "record") then break)
  143.              }
  144.           write(block[2],": ",line)
  145.           }
  146.            "m": {
  147.           #
  148.           #  List the procedure using "more".
  149.           #
  150.           write()
  151.           p := open("more","pw") | stop("Can't popen")
  152.           every write(p,!block[1])
  153.           close(p)
  154.               }
  155.            "e" | "v": {
  156.           #
  157.           #  Invoke ex or vi positioned at the first line
  158.           #  of procedure or record.
  159.           #
  160.           system((if cmd == "e" then "ex" else "vi") ||
  161.                 " +" || block[3] || " " || block[2])
  162.           }
  163.            }
  164.         }
  165.      }
  166.      }
  167. end
  168.  
  169. procedure help()
  170.    write(
  171. "\nEnter:_
  172. \n    q                     Quit_
  173. \n    ?                     Display help message (this message)_
  174. \n    <return>              Redisplay the list of procedure and record names_
  175. \n    <number from list>[f] Display the file name and first line of_
  176. \n                          procedure or record_
  177. \n    <number from list>m   Display the procedure or record using \"more\"_
  178. \n    <number from list>e   Invoke \"ex\" positioned to procedure or record_
  179. \n    <number from list>v   Invoke \"vi\" positioned to procedure or record"
  180.    )
  181.    return
  182. end
  183.